function getUrl(){
    var url = window.location.href;
    var path = window.location.pathname;
    var search = window.location.search;
    url = url.replace(path,"/");
    url = url.replace(search,"")
    return url;
}

(function ($) {

    //扩展方法获取url参数
    $.getUrlParam = function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }

    //得到url参数
    var urltype = $.getUrlParam('reurl');

})(jQuery);

$(document).ready(function () {

    const url = getUrl();

    const index = $.getUrlParam('index');
    console.log(index);

    $.ajax({
        url:url+"adminInfo/getClass",
        data:{
            index:index
        },
        type: "get",
        contentType: "application/json",
        success: function(data) {
            if(data=="unLogin"){
                location.href="login.html";
            }else{
                $.each(data, function (i, _class) {
                    console.log(_class);

                    const mode =
                    '<div class="row-fluid">'+
                        '<div class="span2">'+
                            '<div>'+
                                '<p id="id"></p>'+
                            '</div>'+
                        '</div>'+
                        '<div class="span6">'+
                            '<div>'+
                                '<input class="input-xxlarge disabled" id="name" type="text" placeholder="" disabled="">'+
                            '</div>'+
                        '</div>'+
                        '<div class="span4" id="class_active">'+
                            '<div class="span4" id="class_">'+
                                '<button class="btn btn-small btn-primary" id="class" onclick= "">查看</button>'+
                            '</div>'+
                            '<div class="span4" id="editClass_">'+
                                '<button class="btn btn-small btn-inverse" id="editClass" onclick= "">修改</button>'+
                            '</div>'+
                            '<div class="span4" id="deleteClass_">'+
                                '<button class="btn btn-small btn-danger" id="deleteClass" onclick= "">删除</button>'+
                            '</div>'+
                        '</div>'+
                    '</div>';

                    const $obj = $(mode);
                    $obj.find("#id").text(_class.id);
                    $obj.find("#name").val(_class._name);

                    $("#class_body").append($obj);

                })
            }
        },
        error:function(){
            console.log("----------")
        }
    })

    $("#class_body").on('click','#editClass',function(){

        console.log("editMajor");
    
        $(this).parent().parent().parent().find("#name").removeAttr("disabled");
    
        const save = '<div class="span6" id="confirmEdit_"><button class="btn btn-small btn-danger" id="confirmEdit">确认</button></div class="span6">';
        const cancel = '<div class="span6" id="cancelEdit_"><button class="btn btn-small btn-inverse" id="cancelEdit">取消</button></div class="span6">';
    
        const $mode1 = $(save);
        const $mode2 = $(cancel);
    
        $(this).parent().parent().append($mode1);
        $(this).parent().parent().append($mode2);
        $(this).parent().parent().find("#class_").remove();
        $(this).parent().parent().find("#deleteClass_").remove();
        $(this).parent().parent().find("#editClass_").remove();
    });

    $("#class_body").on('click','#cancelEdit',function(){
        $(this).parent().parent().parent().find("#name").attr('disabled',"");

        const m1 = '<div class="span4" id="class_"><button class="btn btn-small btn-primary" id="class" onclick= "">查看</button></div>'
        const m2 = '<div class="span4" id="editClass_"><button class="btn btn-small btn-inverse" id="editClass" onclick= "">修改</button></div>';
        const m3 = '<div class="span4" id="deleteClass_"><button class="btn btn-small btn-danger" id="deleteClass" onclick= "">删除</button></div>';

        const $mode1 = $(m1);
        const $mode2 = $(m2);
        const $mode3 = $(m3);

        $(this).parent().parent().append($mode1);
        $(this).parent().parent().append($mode2);
        $(this).parent().parent().append($mode3);
        $(this).parent().parent().find("#confirmEdit_").remove();
        $(this).parent().parent().find("#cancelEdit_").remove();
    })

    $("#class_body").on('click','#confirmEdit',function(){
        const index = $.getUrlParam('index');
        console.log(index);

        const map ={
            "index":index,
            "id":$(this).parent().parent().parent().find("#id").text(),
            "name":$(this).parent().parent().parent().find("#name").val()
        }

        console.log(map);

        $.ajax({
            url:url+"adminInfo/updateClass",
            data:JSON.stringify(map),
            type: "post",
            contentType: "application/json; charset=utf-8",
            success: function(id) {
                console.log(id);
                location.href="classManage.html?index="+index;
            },
            error:function(id){
                console.log("----------");
                console.log(id);
            }
        })
        
    });

    $("#class_body").on('click','#class',function(){
        console.log("class");
        const id = $(this).parent().parent().parent().find("#id").text();
        console.log(id);
        location.href="classInfo.html?index="+id;
    });

    $("#class_body").on('click','#deleteClass',function(){
        const id = $(this).parent().parent().parent().find("#id").text();
        console.log(id);
        $.ajax({
            url:url+"adminInfo/deleteClass",
            data:{
                index:id,
            },
            type: "get",
            contentType: "application/json; charset=utf-8",
            success: function(id) {
                console.log(id);
                location.href="classManage.html?index="+index;
            },
            error:function(id){
                console.log("----------");
                console.log(id);
            }
        })
    });

    $("#class_body").on('click','#addClass',function(){
        const index = $.getUrlParam('index');
        console.log(index);
        const map ={
            "name":$("#input_name").val(),
            "index":index
        }
    
        console.log(map);
    
    
        $.ajax({
            url:url+"adminInfo/addClass",
            data:JSON.stringify(map),
            type: "post",
            contentType: "application/json; charset=utf-8",
            success: function(id) {
                console.log(id);
                location.href="classManage.html?index="+index;
            },
            error:function(id){
                console.log("----------");
                console.log(id);
            }
        })
    });

});

$("#add_Class").click(function () {
    const mode = 
    '<div class="row-fluid">'+
        '<div class="span2">'+
            '<div>'+
                
            '</div>'+
        '</div>'+
        '<div class="span8">'+
            '<div>'+
                '<input class="input-xxlarge disabled" id="input_name" type="text" placeholder="">'+
            '</div>'+
        '</div>'+
        '<div class="span2" id="class_active">'+
            '<button class="btn btn-small btn-primary" id="addClass" onclick= "">添加</button>'+
        '</div>'+
    '</div>';
                
    const $obj = $(mode);
    $("#class_title").after($obj);
});